package com.xbongbong.pro.statistic.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.pro.domain.entity.ChartEntity;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.join.query.HasParentQueryBuilder;
import org.elasticsearch.join.query.JoinQueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.data.domain.PageRequest;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * 自定义图表子表单数据处理
 * @author zhouwq
 * @date 2020/10/20 14:31
 */

public abstract class AbstractBiSubFormDataHelp {

    @Resource
    protected CustomHelp customHelp;
    @Resource
    protected XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;

    /**
     * 处理子表单数据组装-es版
     * @param esList 查询结果集
     * @param chartEntity chart实体
     * @param pageHelper 分页
     * @param tableHead 表头
     * @param boolQueryBuilder 查询builder
     * @param searchRequest es API
     * @throws XbbException error
     * @throws IllegalAccessException error
     * @author zhouwq
     * @date 2020/10/20 15:13
     */
    public abstract void handleSubFormData(List esList, ChartEntity chartEntity, PageHelper pageHelper,
                                    JSONArray tableHead, BoolQueryBuilder boolQueryBuilder, SearchRequest searchRequest) throws XbbException, IllegalAccessException;
    
    /**
     * 查询语句拼装
     * @param type 索引type
     * @param corpid 公司ID
     * @param pageHelper 分页
     * @param boolQueryBuilder 查询builder
     * @param searchSubFormRequest es API
     * @return 分页信息
     */
    static PageRequest subFormSearchQueryBuild(String type, String corpid, PageHelper pageHelper, BoolQueryBuilder boolQueryBuilder, SearchRequest searchSubFormRequest) {
        SearchSourceBuilder sourceSubFormBuilder = new SearchSourceBuilder();
        PageRequest pageSubFormRequest = EsUtil.setPage(sourceSubFormBuilder, pageHelper.getCurrentPageNum(), pageHelper.getPageSize());
        BoolQueryBuilder boolSubFormQueryBuilder = boolQuery();
        boolSubFormQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, "sub_form_data"));
        boolSubFormQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolSubFormQueryBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
        HasParentQueryBuilder hasParentQueryBuilder = JoinQueryBuilders.hasParentQuery(type, boolQueryBuilder, false);
        boolSubFormQueryBuilder.filter(hasParentQueryBuilder);
        //执行查询，得到结果
        sourceSubFormBuilder.query(boolSubFormQueryBuilder);
        searchSubFormRequest.source(sourceSubFormBuilder);
        return pageSubFormRequest;
    }

    /**
     * 子表单字段对应展示字段集合关系对应
     * @param tableHead 表头信息
     * @return Map<String, List<String>> 子表单字段 -> 展示字段集合
     */
    static Map<String, List<String>> subFormAttrListGet(JSONArray tableHead) {
        Map<String, List<String>> subFormAttrList = new HashMap<>(1 << 4);
        for (int i = 0; i < tableHead.size(); i++) {
            JSONObject tableObj = tableHead.getJSONObject(i);
            if (FieldTypeEnum.isSubFormType(tableObj.getInteger("fieldType"))) {
                JSONArray subFormAttrArray = tableObj.getJSONArray("items");
                List<String> attr = new ArrayList<>();
                for (int j = 0; j < subFormAttrArray.size(); j++) {
                    JSONObject object = subFormAttrArray.getJSONObject(j);
                    String key = object.getString("key");
                    attr.add(key.split("&")[0]);
                }
                subFormAttrList.put(tableObj.getString("key").split("&")[0], attr);
            }
        }
        return subFormAttrList;
    }
}
